/*
 * Copyright 2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.workers.internal

import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import org.gradle.integtests.fixtures.DefaultTestExecutionResult
import org.gradle.util.Requires
import org.gradle.util.TestPrecondition
import spock.lang.Issue

import static org.hamcrest.CoreMatchers.containsString

/**
 * Ensures test behavior and actual application behavior are equivalent when
 * production code attempts to perform reflection on JDK internals.
 */
class JdkIllegalReflectionTestWorkerIntegrationTest extends AbstractIntegrationSpec {

    def setup() {
        buildFile << """
            plugins {
                id 'application'
            }

            ${mavenCentralRepository()}

            testing.suites.test.useJUnit()

            application {
                mainClass = 'example.Main'
            }
        """

        file("src/main/java/example/Main.java") << """
            package example;

            import java.lang.invoke.MethodHandles;

            public class Main {
                public static void main(String[] args) throws Exception {
                    MethodHandles.privateLookupIn(java.lang.ClassLoader.class, MethodHandles.lookup());
                }
            }
        """

        file("src/test/java/example/MainTest.java") << """
            package example;

            import org.junit.Test;

            public class MainTest {
                @Test
                public void runTest() throws Exception {
                    Main.main(new String[0]);
                }
            }
        """
    }

    @Requires(TestPrecondition.JDK16_OR_LATER)
    @Issue("https://github.com/gradle/gradle/issues/19771")
    def "both tests and production code fail when application uses illegal reflection"() {
        when:
        executer.withStackTraceChecksDisabled()

        then:
        fails "run"
        result.assertHasErrorOutput("module java.base does not open java.lang to unnamed module")

        expect:
        fails "test"
        def results = new DefaultTestExecutionResult(file("."))
        results.assertTestClassesExecuted("example.MainTest")
        results.testClass("example.MainTest").assertTestFailed("runTest", containsString('module java.base does not open java.lang to unnamed module'))
    }

    @Requires(TestPrecondition.JDK16_OR_LATER)
    @Issue("https://github.com/gradle/gradle/issues/19771")
    def "can add --add-opens flag in test to permit reflection"() {
        given:
        buildFile << """
            tasks.withType(Test).configureEach {
                jvmArgs("--add-opens", "java.base/java.lang=ALL-UNNAMED")
            }
        """

        expect:
        succeeds "test"
    }
}
